import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class InfoSection extends StatelessWidget {
  final String outputPath;
  final String projectName;
  final String packageName;
  final String displayName;
  final String flutterSdkPath;
  final String projectType;
  final String templateType;
  final String ideChoice;
  final Function(String) onOutputPathChanged;
  final Function(String) onProjectNameChanged;
  final Function(String) onPackageNameChanged;
  final Function(String) onDisplayNameChanged;
  final Function(String) onFlutterSdkPathChanged;
  final Function(String) onProjectTypeChanged;
  final Function(String) onTemplateTypeChanged;
  final Function(String) onIdeChoiceChanged;
  final VoidCallback onSelectOutputDirectory;
  final VoidCallback onSelectFlutterSdkPath;

  const InfoSection({
    super.key,
    required this.outputPath,
    required this.projectName,
    required this.packageName,
    required this.displayName,
    required this.flutterSdkPath,
    required this.projectType,
    required this.templateType,
    required this.ideChoice,
    required this.onOutputPathChanged,
    required this.onProjectNameChanged,
    required this.onPackageNameChanged,
    required this.onDisplayNameChanged,
    required this.onFlutterSdkPathChanged,
    required this.onProjectTypeChanged,
    required this.onTemplateTypeChanged,
    required this.onIdeChoiceChanged,
    required this.onSelectOutputDirectory,
    required this.onSelectFlutterSdkPath,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Padding(
        padding: EdgeInsets.all(16.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "项目信息",
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                Expanded(
                  child:
                      _buildTextField("输出目录", outputPath, onOutputPathChanged),
                ),
                SizedBox(width: 8.w),
                IconButton(
                  onPressed: onSelectOutputDirectory,
                  icon: const Icon(Icons.folder_open),
                  tooltip: "选择目录",
                  style: IconButton.styleFrom(
                    backgroundColor: Colors.blue.withOpacity(0.1),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 12.h),
            _buildTextField("项目名称", projectName, onProjectNameChanged),
            SizedBox(height: 12.h),
            _buildTextField("包名", packageName, onPackageNameChanged),
            SizedBox(height: 12.h),
            _buildTextField("显示名称", displayName, onDisplayNameChanged),
            SizedBox(height: 12.h),
            Row(
              children: [
                Expanded(
                  child: _buildTextField(
                      "Flutter SDK路径", flutterSdkPath, onFlutterSdkPathChanged),
                ),
                SizedBox(width: 8.w),
                IconButton(
                  onPressed: onSelectFlutterSdkPath,
                  icon: const Icon(Icons.folder_open),
                  tooltip: "选择Flutter SDK目录",
                  style: IconButton.styleFrom(
                    backgroundColor: Colors.blue.withOpacity(0.1),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                Expanded(
                  child: _buildDropdown(
                    "项目类型",
                    projectType,
                    ["app", "module"],
                    (value) {
                      if (value != null) {
                        onProjectTypeChanged(value);
                      }
                    },
                  ),
                ),
                SizedBox(width: 16.w),
                Expanded(
                  child: _buildDropdown(
                    "模板类型",
                    templateType,
                    ["1", "2"],
                    (value) {
                      if (value != null) {
                        onTemplateTypeChanged(value);
                      }
                    },
                    labelMap: {"1": "基础版", "2": "增强版"},
                  ),
                ),
                SizedBox(width: 16.w),
                Expanded(
                  child: _buildDropdown(
                    "打开方式",
                    ideChoice,
                    ["open_folder", "vscode", "android_studio"],
                    (value) {
                      if (value != null) {
                        onIdeChoiceChanged(value);
                      }
                    },
                    labelMap: {
                      "open_folder": "打开文件夹",
                      "vscode": "VSCode",
                      "android_studio": "Android Studio"
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 构建文本输入框
  Widget _buildTextField(
      String label, String value, Function(String) onChanged) {
    return TextField(
      decoration: InputDecoration(
        labelText: label,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8.r),
        ),
        contentPadding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.h),
      ),
      controller: TextEditingController(text: value),
      onChanged: onChanged,
    );
  }

  // 构建下拉选择框
  Widget _buildDropdown(String label, String value, List<String> items,
      Function(String?) onChanged,
      {Map<String, String>? labelMap}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14.sp,
            color: Colors.grey[700],
          ),
        ),
        SizedBox(height: 8.h),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
            borderRadius: BorderRadius.circular(8.r),
          ),
          child: DropdownButton<String>(
            value: value,
            isExpanded: true,
            underline: Container(),
            items: items.map((String item) {
              return DropdownMenuItem<String>(
                value: item,
                child: Text(labelMap != null ? (labelMap[item] ?? item) : item),
              );
            }).toList(),
            onChanged: onChanged,
          ),
        ),
      ],
    );
  }
}
